#include "Camera.h"
#include <cmath>
#include <cstdlib>

  //-----------------------------------------------------//
 //             Constructor/Destructor                  //
//-----------------------------------------------------//
Camera::Camera(int x, int y, int w, int h)
{
	this->x = x;
	this->y = y;
	this->w = w; //could be NULL
	this->h = h; //could be NULL
	
	this->x_to = x;
	this->y_to = y;
	this->speed = 15.f;
	this->moving = false;

	this->shaking = false;
	this->shake_x = 0;
	this->shake_y = 0;
}
Camera::~Camera(void)
{
}

  //-----------------------------------------------------//
 //                     Update                          //
//-----------------------------------------------------//
void Camera::update()
{
	///Shake camera
	if (this->shaking == true)
	{
		int time_running = this->shake_timer.getTicks();
		if (time_running < this->shake_duration)
		{		
			float pow = this->shake_power;
			if (time_running < this->shake_fadeInDuration)
				pow *= (time_running / this->shake_fadeInDuration);
			else if (time_running >= this->shake_duration - this->shake_fadeOutDuration)
				pow *= (this->shake_duration - time_running) / this->shake_fadeOutDuration;
			
			this->shake_x = rand() * 64.f * pow;
			this->shake_y = rand() * 64.f * pow;
		}
		else
		{
			this->shaking = false;
			this->shake_x = 0;
			this->shake_y = 0;			
		}
	}

	//Move camera
	if (this->moving == true)
	{
		float direction = atan2(this->y_to - this->y, this->x_to - this->x);
		this->x += cos(direction) * this->speed;
		this->y += sin(direction) * this->speed;
		if (abs(this->x_to - this->x) < this->speed && 
			abs(this->y_to - this->y) < this->speed)
		{
			this->x = this->x_to;
			this->y = this->y_to;
			this->moving = false;
		}
	}
}

  //-----------------------------------------------------//
 //                     Get - Set                       //
//-----------------------------------------------------//
void Camera::move_x(int x_to)
{
	if (x_to == this->x)
	{
		this->moving = true;
		this->x_to = x_to;
	}
}
void Camera::move_y(int y_to)
{
	if (y_to == this->y)
	{
		this->moving = true;
		this->y_to = y_to;
	}
}
void Camera::move_to(int x, int y)
{
	this->move_x(x);
	this->move_y(y);
}
void Camera::set_speed(float speed)
{
	this->speed = speed;
}
void Camera::set_x(int x_to)
{
	this->x = x_to;
}
void Camera::set_y(int y_to)
{
	this->y = y_to;
}
void Camera::set_pos(int x, int y)
{
	this->set_x(x);
	this->set_y(y);
}
int Camera::get_x()
{
	return this->x + this->shake_x;
}
int Camera::get_y()
{
	return this->y + this->shake_y;
}
void Camera::shake(float power, int duration, bool fadeIn, 
				   int fadeInDuration, bool fadeOut, int fadeOutDuration)
{
	this->shake_power = power;
	this->shake_duration = duration;
	this->shake_fadeIn = fadeIn;
	this->shake_fadeOut = fadeOut;
	this->shake_fadeInDuration = fadeInDuration;
	this->shake_fadeOutDuration = fadeOutDuration;

	if (this->shake_fadeInDuration <= 0)
		this->shake_fadeIn = false;

	if (this->shake_fadeOutDuration <= 0)
		this->shake_fadeOut = false;

	this->shake_timer.start();
	this->shaking = true;
}